package cn.buk.qms.web;

import cn.buk.common.JsonResult;
import cn.buk.common.dto.CommonDto;
import cn.buk.common.sc.CommonSearchCriteria;
import cn.buk.tms.entity.order.OrderComment;
import cn.buk.qms.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author yfdai
 *
 * 订单评论
 */
@RestController
public class CommentController extends BaseController {

  @Autowired
  private CommentService commentService;

  /**
   * 创建订单评论
   */
  @PostMapping(value = "/comment")
  public JsonResult createOrderComment(HttpServletRequest request,
                                       @RequestParam("orderType") final int orderType,
                                       @RequestParam("orderId") final int orderId,
                                       @RequestParam("content") final String content
  ) {
    final int enterpriseId = getEnterpriseId(request);
    final String operator = getUsername(request);

    return commentService.createComment(enterpriseId, operator, orderType, orderId, content);
  }

  /**
   * 查找订单评论
   */
  @RequestMapping("/comments")
  public CommonDto<OrderComment> searchFlightOrders(HttpServletRequest request,
                                                    @RequestParam("orderType") final int orderType,
                                                    @RequestParam("orderId") final int orderId,
                                                    @RequestParam(value = "pageNo", defaultValue = "1") final int pageNo,
                                                    @RequestParam(value = "pageSize", defaultValue = "100") final int pageSize
  ) {
    CommonSearchCriteria sc = new CommonSearchCriteria();
    sc.setPageNo(pageNo);
    sc.setPageSize(pageSize);

    List<OrderComment> list = commentService.searchComments(getEnterpriseId(request), orderType, orderId, sc);

    CommonDto<OrderComment> commonDto = new CommonDto<>();
    commonDto.setDataList(list);
    commonDto.setPage(sc.getPage());

    return commonDto;
  }

}
